<!--
* @Author: zhangjing
* @Date: 2025-04-22 17:51:46
* @Description: 开始上课=>
-->
<template>
	<div>
		<a-modal v-model="showTips" title="提示">
			<p>是否继续上一次课题？</p>
			<template slot="footer">
				<a-button key="back" @click="() => (showTips = false)">取消</a-button>
				<a-button type="primary" :loading="loading" @click="sureClass(0)">重新上课</a-button>
				<a-button type="primary" :loading="loading" @click="sureClass(1)">确定</a-button>
			</template>
		</a-modal>
		<a-form autocomplete="off">
			<div class="input-row flex flex-align-center" style="overflow: visible">
				<label> 班级 </label>
				<search
					:searchList="classList"
					:selectValue.sync="selectclass"
					@selectFunc="changeClass"
					placeholdertxt="请选择班级"
					style="flex: 1"
				></search>
			</div>
			<div class="input-row flex flex-align-center" style="overflow: visible">
				<label> 科目 </label>
				<search
					:searchList="subjectList"
					:selectValue.sync="selectsubject"
					@selectFunc="changeSubject"
					placeholdertxt="请选择班级"
					style="flex: 1"
				></search>
			</div>
			<div class="flex flex-align-center">
				<a-button type="default" class="rebackBtn" @click="cancel">返回</a-button>
				<a-button type="primary" class="loginBtn flex-1" @click="skipPage">确定</a-button>
			</div>
		</a-form>
	</div>
</template>

<script>
import { mapActions, mapState, mapMutations } from 'vuex';
import defaultSettings from '@/config/defaultSettings';
import { PwdReg } from '@/utils/validate';
import md5 from 'md5';
import { sysManageapi, postAction } from '@/api/index.js';
import QS from 'qs';
import search from '@/components/search/search';
let timer = null;
export default {
	data() {
		return {
			loading: false,
			showTips: false,
			classList: [],
			subjectList: [],
			selectclass: {},
			selectsubject: {},
			startClassInfo: {
				className: '',
				classId: '',
				subjectCode: '',
				subjectName: '',
				gradeId: '',
				gradeName: '',
				title: '',
				csId: '',
				deptId: '',
				topicId: '',
				statusRev: 0, //表示是否已经下课
			},
		};
	},
	components: { search },
	mounted() {},
	computed: {},
	created() {
		this.getTeachSubjectList();
		this.getClassList();
		this.findCourseOne();
	},
	methods: {
		...mapMutations(['SET_isAnswering', 'SET_ANS_ID', 'CLEAR_EVENTLIST']),
		cancel() {
			this.$emit('cancel');
		},
		//获取班级
		getClassList() {
			return this.$postAction(this.$sysManageapi.classUrl.getAllClassAndReciverList, {
				orgId: this.globalOrgId || '',
			}).then((res) => {
				if (res && res.code == 1) {
					this.classList = res.data.map((item) => {
						return {
							name: item.gradeName + ' ' + item.className,
							...item,
						};
					});
				}
			});
		},

		//最小权限科目
		getTeachSubjectList() {
			return this.$postAction(this.$sysManageapi.teacherUrl.getTeacherTopPage).then((res) => {
				if (res && res.code == 1) {
					this.subjectList = [];
					if (res.data && JSON.stringify(res.data) != '{}') {
						for (var key in res.data) {
							let item = {
								subjectCode: key,
								subjectName: res.data[key],
								name: res.data[key],
							};
							this.subjectList.push(item);
						}
					}
				}
			});
		},
		//获取该账号最近一次上课记录
		findCourseOne(value) {
			let params = {};
			if (value) {
				params = {
					subjectCode: this.startClassInfo.subjectCode,
					classId: this.startClassInfo.classId,
				};
			}
			return this.$postAction(this.$sysManageapi.courseUrl.findCourseOne, params).then((res) => {
				if (res && res.code == 1) {
					if (res.data) {
						let startClassInfo = this.startClassInfo;
						for (var key in startClassInfo) {
							startClassInfo[key] = res.data[key];
						}
						this.startClassInfo = { ...startClassInfo };
						if (startClassInfo.classId) {
							this.selectclass = {
								classId: startClassInfo.classId,
								className: startClassInfo.className,
								name: startClassInfo.className,
							};
						}
						if (startClassInfo.subjectCode) {
							this.selectsubject = {
								subjectCode: startClassInfo.subjectCode,
								subjectName: startClassInfo.subjectName,
								name: startClassInfo.subjectName,
							};
						}
					} else {
						this.startClassInfo.csId = '';
						this.startClassInfo.statusRev = 0;
					}
				}
			});
		},
		//跳转页面
		async skipPage() {
			console.log('this.startClassInfo', this.startClassInfo);
			if (!this.startClassInfo.classId) {
				this.$message.error('请选择作答班级！');
				return;
			}
			if (!this.startClassInfo.subjectCode) {
				this.$message.error('请选择科目！');
				return;
			}
			this.showSecondsTips();
		},
		/* 二次提醒 */
		async showSecondsTips() {
			console.log('this.startClassInfo', this.startClassInfo);
			/* 如果有csId，则弹框提示是否继续上次课题 */
			if (this.startClassInfo.csId && this.startClassInfo.statusRev == 1) {
				this.showTips = true;
				return;
			}
			this.sureClass(0);
		},
		//确定上课
		/* operateType=0：重新上课1：继续课题 */
		async sureClass(operateType) {
			if (this.loading) {
				return;
			}
			this.showTips = false;
			let isHasSuccessAttend = await this.startClass(this.topicType, operateType);
			if (isHasSuccessAttend) {
				this.CLEAR_EVENTLIST();
				this.SET_ANS_ID('');
				this.SET_isAnswering(0);
				this.$router.push({
					path: '/home',
				});

				if (window.ipcRenderer) {
					window.ipcRenderer.send('onlinedirebro', 1);
				}
			}
		},
		/* operateType=0：重新上课1：继续课题 */
		startClass(topicType, operateType = 0) {
			this.loading = true;
			if (operateType == 0) {
				this.startClassInfo.title = this.startClassInfo.subjectName + moment().format('YYYY-MM-DD HH:mm:ss');
			}
			let paramter = Object.assign(
				{},
				{
					isWire: 1,
				},
				this.startClassInfo,
				{
					csId: operateType == 0 ? '' : this.startClassInfo.csId,
				}
			);

			return new Promise((resolve, reject) => {
				return this.$postAction(this.$sysManageapi.courseUrl.startClass, {
					...paramter,
					isWin: 1, //是否是exe程序上课
				})
					.then((res) => {
						if (res && res.code == 1) {
							let {
								classId,
								className,
								gradeId,
								gradeName,
								subjectCode,
								subjectName,
								title,
								csId,
								deptId,
								alias,
							} = this.startClassInfo;
							res.data.csId = res.data.csId || csId;
							let params = Object.assign(
								{},
								{
									classId,
									className,
									gradeId,
									gradeName,
									subjectCode,
									subjectName,
									title,
									csId,
									deptId,
									alias,
								},
								{
									csId: res.data.csId, //课题id
									startTime: res.data.startTime,
									durLesson: res.data.durLesson,
									topicType: 0,
								}
							);
							//存储上课必须数据
							this.$ls.set(
								'beginClass',
								Object.assign({}, params, {
									isBegin: true,
								})
							);
							resolve(res.code);
						} else {
							resolve();
						}
					})
					.catch((err) => {
						//返回错误
						reject(err);
					})
					.finally((res) => {
						setTimeout(() => {
							this.loading = false;
						}, 200);
					});
			});
		},
		changeClass(value) {
			let startClassInfo = this.startClassInfo;
			let classInfo = { ...value };
			for (var key in startClassInfo) {
				if (key != 'subjectCode' && key != 'subjectName') {
					startClassInfo[key] = classInfo[key] || '';
				}
			}
			startClassInfo.csId = '';
			this.startClassInfo = { ...startClassInfo };
			this.findCourseOne(1);
			console.log('startClassInfo', startClassInfo);
		},
		async changeSubject(value) {
			this.startClassInfo.subjectCode = value.subjectCode;
			this.startClassInfo.subjectName = value.subjectName;
			this.findCourseOne(1);
		},
	},
};
</script>

<style scoped="scoped" lang="less">
.bg {
	position: absolute;
	height: 100%;
	width: 100%;
	left: 0;
	right: 0;

	.loginbox {
		background: rgba(255, 255, 255, 0.7);
		position: absolute;
		left: 50%;
		top: 50%;
		transform: translate(-50%, -50%);
		width: 9rem;
		overflow: hidden;
		.loginbox-hd {
			border-radius: 10px 10px 0 0;
			background: @primary-color;
			height: 0.85rem;
			border: 1px solid #4d5053;
			position: relative;
			&::before,
			&::after {
				content: '';
				display: block;
				width: 0.2rem;
				height: 0.2rem;
				background: #ffe95d;
				border: 1px solid #4d5053;
				border-radius: 100px;
				position: absolute;
				right: 0.2rem;
				top: 50%;
				transform: translateY(-50%);
			}
			&::before {
				left: 0.2rem;
			}
		}
		.loginbox-bd {
			background: #f3f8ff;
			border: 1px solid #4d5053;
			border-top: none;
			height: 5.1rem;
		}
		.input-group {
			width: 5rem;
			margin: 0 auto;
			&.input-group-lg {
				width: 6rem;
			}
			.input-row {
				line-height: 0.8rem;
				overflow: hidden;
				border: 1px solid @primary-color;
				border-radius: 1rem;
				position: relative;
				font-size: 0.24rem;
				background: #fff;
				padding-right: 0.36rem;
				i.icon {
					display: block;
					width: 0.36rem;
					height: 0.36rem;
					background: no-repeat center center;
					background-size: 100% auto;
					margin: 0 0.4rem;

					&.user {
						background-image: url(/img/user.png);
					}

					&.pwd {
						background-image: url(/img/password.png);
					}
				}
				label {
					color: #2e2424;
					padding: 0 0.4rem;
					font-size: 0.3rem;
				}
			}
		}

		.input-row + .input-row {
			margin-top: 0.24rem;
		}

		.loginBtn,
		.rebackBtn {
			height: 0.8rem;
			display: block;
			background: @primary-color;
			text-align: center;
			color: #fff;
			line-height: 0.8rem;
			margin-top: 0.4rem;
			width: 100%;
			border: none;
			font-size: 0.24rem;
			border-radius: 100px;
		}
		.rebackBtn {
			background: #becad4;
			color: #2e2424;
			width: 1.6rem;
			margin-right: 0.16rem;
		}
	}
}
</style>
